#include "task_app_logic.h"
static TaskHandle_t thread_app_dazzling;
app_status_info_t app_dazzling_status; //保存数据用的数据结构
void brighness_recover() //亮度恢复
{
  u8 brigh_level;
  if(driver_nvs_part_read_u8(BRIGH_LEVEL,&brigh_level) == ESP_OK) //恢复RGB灯带灯带亮度
  {
    init_brighness_level(brigh_level);
  }
  else
  {
    init_brighness_level(LEVEL);//预设亮度
  }
}


void micro_about_recover() //麦克风恢复
{
  u8 micro_senst;
  u8 spectral_mode;
  if (driver_nvs_part_read_u8(MICROSENST,&micro_senst) == ESP_OK) //恢复灵敏度
  {
    set_senst_level(micro_senst);
  }
  else
  {
    set_senst_level(100);
  }

  if(driver_nvs_part_read_u8(SPECTRALMODE,&spectral_mode) == ESP_OK) //恢复子状态
  {
    set_micro_ssp(spectral_mode);
  }
  else
  {
    set_micro_ssp(MICRO_ENERGY);
  }
}

void scene_recover()//恢复场景子状态
{
  u8 scene_mdoe;
  if(driver_nvs_part_read_u8(MODESCENE,&scene_mdoe) == ESP_OK) 
  {
    set_scene_ssp(scene_mdoe);
  }
  else
  {
    set_scene_ssp(AUROAR_MODE);
  }
}

void custom_ssp_recover() //场景子模式初始化
{
  u8 custom_ssp;
  if(driver_nvs_part_read_u8(CUSTOMSSP,&custom_ssp) == ESP_OK) //恢复RGB灯带灯带亮度
  {
    set_custom_ssp(custom_ssp);
  }
  else
  {
    set_custom_ssp(0x00); //强制初始化 虽然无效
  }
}

void part_recover() //分段模式初始化
{
  if(read_partition(PART_LOCA,&part_info,sizeof(PART_INFO_T)) == ESP_OK)
  {
    printf("read part_info success\n");
    printf("len is %d\n",part_info.len);
    if(part_info.len == 0x00|| part_info.len ==0xFF)
    {
      memset(&part_info,0,sizeof(PART_INFO_T));
    }
    else
    {
      for (size_t i = 0; i < part_info.len*3; i++)
      {
        printf("%x ",part_info.RGB[i]);
      }
      printf("\n");
    }

  }
}

void cw_mode_config_recover()
{
  u8 cw,brighness;
    if(driver_nvs_part_read_u8(CW_LEVEL,&cw) == ESP_OK) //恢复cw值
    {
      printf("cw level is %d\n",cw);
    }
    else
    {
      cw = 50;
    }

    if(driver_nvs_part_read_u8(CW_BRIGH,&brighness) == ESP_OK) //恢复cw值
    {
      printf("cw level is %d\n",brighness);
    }
    else
    {
      brighness = 50;
    }

    init_cw_level(cw,brighness);
}


u8 app_logic_init()
{
    app_logic_set_ssp(SSP_LOGO); //让设备进入开机动画模式
    app_logic_set_last_ssp(SSP_NONE); //将初始化化模式设置为上电模式
    set_light_length(LED_NUMBER); //初始化40个灯珠
    brighness_recover(); //亮度调节
    micro_about_recover(); //麦克风恢复
    scene_recover();//恢复场景子状态
    custom_ssp_recover(); //场景子模式初始化
    cw_mode_config_recover(); //cw模式配置初始化
    part_recover(); //分段模式初始化
    init_net_light_handle_func();
    
    
    memset(&app_dazzling_status,0,sizeof(app_status_info_t));
    return 1;
}




void app_logic_general_handle(SYS_OPT_KEY opt)
{
    mode_net_handle(opt);
    if((app_logic_get_ssp() == SSP_MODE_CW) && (app_logic_get_ssp() != SSP_POWER_OFF) && (app_logic_get_ssp() != SSP_LOGO))
    {
      cw_brilliance_control_handle(opt);
    }
    else if ((app_logic_get_ssp() != SSP_MODE_CW) && (app_logic_get_ssp() != SSP_POWER_OFF) && (app_logic_get_ssp() != SSP_LOGO))
    {
      brilliance_control_handle(opt);
    }
    mode_handle(opt);
}


void app_logic_status_save()
{
    static int time_part =0;
    static int time_all =0;

    if(arch_os_ms_elapsed(time_all) >(5*1000)) //计时5s
    {
        time_all =arch_os_ms_now();
        //主模式
        if((app_dazzling_status.main_ssp != app_logic_get_ssp())&&(SSP_LOGO != app_logic_get_ssp())&&(SSP_POWER_OFF != app_logic_get_ssp())&&(SSP_HIGH_SPEED != app_logic_get_ssp()))
        {
            if (app_logic_get_ssp() !=SSP_MODE_CUSTOM)
            {
                app_dazzling_status.main_ssp = app_logic_get_ssp();
                driver_nvs_part_write_u8(APP_STATUS,(u8 *)&app_dazzling_status.main_ssp);
                LOG_INFO("app_dazzling_status.main_ssp IS %d\n",app_dazzling_status.main_ssp);
            }
            else if ((get_light_effect_enum() != 0x00)&&(get_light_effect_enum() != 0xFF))
            {
                app_dazzling_status.main_ssp = app_logic_get_ssp();
                driver_nvs_part_write_u8(APP_STATUS,(u8 *)&app_dazzling_status.main_ssp);
                LOG_INFO("app_dazzling_status.main_ssp IS %d\n",app_dazzling_status.main_ssp);
            }
        }
        
        /*CW 亮度 */
        if(app_dazzling_status.cw_date_config.w != get_cw_config_w())
        {
            app_dazzling_status.cw_date_config.w = get_cw_config_w();
            driver_nvs_part_write_u8(CW_LEVEL,(u8 *)&app_dazzling_status.cw_date_config.w);
        } //色温
        
        if(app_dazzling_status.cw_date_config.brighness != get_cw_config_brighness())
        {
            app_dazzling_status.cw_date_config.brighness = get_cw_config_brighness();
            driver_nvs_part_write_u8(CW_BRIGH,(u8 *)&app_dazzling_status.cw_date_config.brighness);
        } //亮度

        //RGB亮度
        if(app_dazzling_status.brighness_level != get_brighness_level())
        {
            app_dazzling_status.brighness_level=get_brighness_level();
            driver_nvs_part_write_u8(BRIGH_LEVEL,(u8 *)&app_dazzling_status.brighness_level);
            LOG_INFO("app_dazzling_status.brighness_level IS %d\n",app_dazzling_status.brighness_level);
        }//亮度调节

        /***********************************麦克风相关********************************************/
        if(app_dazzling_status.micro_senst != get_senst_level())
        {
            app_dazzling_status.micro_senst = get_senst_level();
            driver_nvs_part_write_u8(MICROSENST,(u8 *)&app_dazzling_status.micro_senst);
            LOG_INFO("app_dazzling_status.micro_senst is %d\n",app_dazzling_status.micro_senst);
        }//麦克风灵敏度

        if(app_dazzling_status.spectral_mode != get_micro_ssp())
        {
            app_dazzling_status.spectral_mode = get_micro_ssp();
            driver_nvs_part_write_u8(SPECTRALMODE,(u8 *)&app_dazzling_status.spectral_mode);
            LOG_INFO("app_dazzling_status.spectral_mode is %d\n",app_dazzling_status.spectral_mode);
        }//麦克风子模式

        /************************************场景模式************************************************************/
        if(app_dazzling_status.scene_mode != get_scene_ssp())
        {
            app_dazzling_status.scene_mode = get_scene_ssp();
            driver_nvs_part_write_u8(MODESCENE,(u8 *)&app_dazzling_status.scene_mode);
            LOG_INFO("app_dazzling_status.scene_mode is %d\n",app_dazzling_status.scene_mode);
        }//场景模式子模式

        if(app_dazzling_status.scene_senet != get_scene_senet())
        {
            app_dazzling_status.scene_senet = get_scene_senet();
            driver_nvs_part_write_u8(scene_senst_save[get_scene_ssp()],&app_dazzling_status.scene_senet);
            LOG_INFO("app_dazzling_status.scene_senet save is%d\n",app_dazzling_status.scene_senet);
        }//场景模式灵敏度

        /*************************************自定义模式**********************************************************/
        if(app_dazzling_status.custom_ssp != get_custom_ssp())
        {
            if((get_light_effect_enum() != 0x00)&&(get_light_effect_enum() != 0xFF))
            {
                app_dazzling_status.custom_ssp = get_custom_ssp();
                driver_nvs_part_write_u8(CUSTOMSSP,(u8 *)&app_dazzling_status.custom_ssp);
                LOG_INFO("app_dazzling_status.custom_ssp is %d\n",app_dazzling_status.custom_ssp);
            }
        }

        

    }

    if (arch_os_ms_elapsed(time_part) >(20*1000)) //计时20s
    {
        time_part =arch_os_ms_now();
        /************************************分段模式**************************************************/
        if((app_dazzling_status.part_info.len != part_info.len) ||(memcmp(app_dazzling_status.part_info.RGB,part_info.RGB,part_info.len*3)))
        {
            app_dazzling_status.part_info.len = part_info.len;
            memcpy(app_dazzling_status.part_info.RGB,part_info.RGB,part_info.len*3);
            if (write_partition(PART_LOCA,&part_info,part_info.len*3+1) == ESP_OK)
            {
                printf("save part_info success\n");
                printf("len is %d\n",part_info.len);

                for (size_t i = 0; i < part_info.len*3; i++)
                {
                    printf("%x ",part_info.RGB[i]);
                }
                printf("\n");
            }
        }
    }
}

void app_logic_loop(void *pvParameters)
{
    key_act_param key_act;
    SYS_OPT_KEY opt;
    app_logic_init(); //通用初始化
    while(1)
    {

      opt.oepation = OPT_NONE;
      if(xQueueReceive(sensor_dect_queue,&key_act,0)) //获取按键状态
      {
        LOG_INFO("app logic task : recv msg,type is %d,act_source num is %d\n",key_act.drvAct,key_act.act_source); //接收到按键操作动作
        opt=app_logic_act_to_opt(key_act); //将按键状态转换为操作状态
      }
      
      app_logic_general_handle(opt); //逻辑切换
      app_logic_status_save();
      vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS)); //延时10ms
    }
}

void task_app_logic_init()
{
    xTaskCreate(app_logic_loop,"app_logic_loop",APP_LOGIC_SIZE,NULL,TASK_PROP_APP,&thread_app_dazzling);
}